<?php
session_start();
include(dirname(__FILE__) . '/../check_login.php');
include(dirname(__FILE__) . '/../config.php');
require(dirname(__FILE__) . '/../header.php');
require_once('users_lib.php');

include_once(dirname(__FILE__) . '/../module/modules_lib.php');
//$trees=get_tree_module($_SESSION['id']);
$modules_arr = fetch_children(0);

$user = get_user_detail($_REQUEST['id']);


if (isset($_POST['go'])) {
    //update_user($_POST);
    //var_dump($_POST);
//    die();
    delete_user_acl($_POST['id']);


    foreach ($_POST as $key => $val) {
        if ($key != 'go') {
            $arr = explode('_', $key);
            if (isset($arr[2])) {
                $module_id = $arr[2];
            } else {
                $module_id = 0;
            }

            if ($module_id != 0) {
                $permitted_action = $arr[1];
                //echo $permitted_action . "<br/>";
                $new_post['user_id'] = $_POST['id'];
                $new_post['module_id'] = $module_id;
                insert_user_acl($new_post);
                if ($permitted_action == 'view') {
                    $new_post['can_view'] = 1;
                    $new_post['user_id'] = $_POST['id'];
                    $new_post['module_id'] = $module_id;
                    update_user_acl($new_post);
                    unset($new_post);
                }

                if ($permitted_action == 'add') {
                    $new_post['can_add'] = 1;
                    $new_post['user_id'] = $_POST['id'];
                    $new_post['module_id'] = $module_id;
                    update_user_acl($new_post);
                    unset($new_post);
                }

                if ($permitted_action == 'update') {
                    $new_post['can_update'] = 1;
                    $new_post['user_id'] = $_POST['id'];
                    $new_post['module_id'] = $module_id;
                    update_user_acl($new_post);
                    unset($new_post);
                }


                if ($permitted_action == 'delete') {
                    $new_post['can_delete'] = 1;
                    $new_post['user_id'] = $_POST['id'];
                    $new_post['module_id'] = $module_id;
                    update_user_acl($new_post);
                    unset($new_post);
                }
            }
        }
    }
}


//$normal_users = get_all_normal_users();
?>
<!-- BEGIN CONTAINER -->
<div class="page-container">
    <!-- BEGIN SIDEBAR -->
    <?php require_once(dirname(__FILE__) . '/../left_menu.php'); ?>
    <!-- END SIDEBAR -->
    <!-- BEGIN CONTENT -->
    <div class="page-content-wrapper">
        <div class="page-content">
            <!-- /.modal -->
            <!-- END SAMPLE PORTLET CONFIGURATION MODAL FORM-->
            <!-- BEGIN STYLE CUSTOMIZER -->

            <!-- END STYLE CUSTOMIZER -->
            <!-- BEGIN PAGE HEADER-->
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN PAGE TITLE & BREADCRUMB-->
                    <ul class="page-breadcrumb breadcrumb">
                        <li>
                            <i class="fa fa-home"></i>
                            <a href="<?php echo $url?>/dashboard.php">
                                Trang chủ
                            </a>
                            <i class="fa fa-angle-right"></i>
                        </li>
                        <li>
                            <a href="#">
                                Màn hình quản trị
                            </a>
                        </li>

                    </ul>
                    <!-- END PAGE TITLE & BREADCRUMB-->
                </div>
            </div>
            <!-- END PAGE HEADER-->
            <!-- BEGIN PAGE CONTENT-->
            <div class="row">
                <div class="col-md-12">
                    <!-- BEGIN EXAMPLE TABLE PORTLET-->
                    <div class="portlet box green">
                        <div class="portlet-title">
                            <div class="caption">
                                <i class="fa fa-cogs"></i>Cấu hình quyền truy cập
                                cho <?php echo $user['first_name'] ?> <?php echo $user['last_name'] ?>
                            </div>

                        </div>
                        <div class="portlet-body">
                            <form action="config_user_acl.php" method="post">
                                <input type="hidden" name="id" value="<?php echo (isset($user['id']) ? $user['id'] : '') ?>"/>
                                <div class="row">

                                    <div class="btn-group pull-right col-sm-12">
                                        <input id="send_all_birthday" type="submit" class="btn green" name="go" value="Cập nhật"/>


                                    </div>
                                </div>
                                <table class="table table-striped table-bordered table-hover" id="upcomming_birthday_list">
                                    <thead>
                                        <tr>

                                            <th>
                                                Module
                                            </th>
                                            <th>
                                                Được xem
                                            </th>

                                            <th>
                                                Được thêm mới
                                            </th>

                                            <th>
                                                Được sửa
                                            </th>
                                            <th>
                                                Được xóa
                                            </th>
                                        </tr>
                                    </thead>
                                    <tbody>

                                        <?php
                                        foreach ($modules_arr as $module_arr) {
                                            $permission = get_permission($_REQUEST['id'], $module_arr);
                                            ?>
                                            <tr>

                                                <td>
                                                    <?php
                                                    $module = get_module_detail($module_arr);
                                                    if ($module['level'] > 0) {
                                                        for ($i = 0; $i < $module['level']; $i++) {
                                                            echo '&nbsp;&nbsp;';
                                                        }
                                                    }

                                                    echo $module['name'];
                                                    ?>
                                                </td>
                                                <td>
                                                    <input type="checkbox" <?php echo ((isset($permission['can_view'])&&$permission['can_view']==1) ? 'checked="checked"' : '') ?> name="id_view_<?php echo $module['id'] ?>" class="checkboxes" value="1"/>
                                                </td>

                                                <td>
                                                    <input type="checkbox" <?php echo ((isset($permission['can_add'])&&$permission['can_add']==1) ? 'checked="checked"' : '') ?> name="id_add_<?php echo $module['id'] ?>" class="checkboxes" value="1"/>
                                                </td>
                                                <td>
                                                    <input type="checkbox" <?php echo ((isset($permission['can_update'])&&$permission['can_update']==1) ? 'checked="checked"' : '') ?> name="id_update_<?php echo $module['id'] ?>" class="checkboxes" value="1"/>
                                                </td>
                                                <td>
                                                    <input type="checkbox" <?php echo ((isset($permission['can_delete'])&&$permission['can_delete']==1) ? 'checked="checked"' : '') ?> name="id_delete_<?php echo $module['id'] ?>" class="checkboxes" value="1"/>
                                                </td>
                                            </tr>


<?php } ?>


                                    </tbody>
                                </table>
                                <div class="row">

                                    <div class="btn-group pull-right col-sm-12">
                                        <input id="send_all_birthday" type="submit" class="btn green" name="go" value="Cập nhật"/>


                                    </div>
                                </div>    
                            </form>

                        </div>

                    </div>
                    <!-- END EXAMPLE TABLE PORTLET-->
                </div>
            </div>

            <!-- END PAGE CONTENT-->
        </div>
    </div>
    <!-- END CONTENT -->
</div>
<!-- END CONTAINER -->
<!-- BEGIN FOOTER -->
<?php
require_once(dirname(__FILE__) . '/../footer.php');
require_once(dirname(__FILE__) . '/../common_script.php');
?>

<!-- BEGIN PAGE LEVEL SCRIPTS -->
<script src="<?php echo $url ?>/assets/scripts/core/app.js"></script>

<script>
    jQuery(document).ready(function () {
        App.init();
        //TableManaged.init();
<?php
if (isset($_POST['go'])) {
    ?>
            bootbox.alert("Cập nhật dữ liệu thành công! :)", function () {
                document.location.href = 'users.php';
            });
    <?php
}
?>
    });

</script>
</body>
<!-- END BODY -->
</html>